package main;

import cn.hutool.core.util.ZipUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.db.ds.druid.DruidDSFactory;
import entity.Pubmed;
import lombok.SneakyThrows;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import utils.PubmedUtils2;
import utils.SmallTool;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 葛成瑞
 * @crete 2021/11/18 15:45
 */
public class insertxmltopmtable {

    static String dbName = "xml_content_1_3qw";
    //    static String dbName = "pubmed_xml_4_5wan";
    //    static String insetTable = "pubmed_vip_extend";
    static String insetTable = "pubmed_vip_extend_new";
    // 全部插入不判断 如果是true 则进行全部插入
    static boolean insert = true;
    //通过id进行开始


    @SneakyThrows
    public static void main(String[] args) {
        //  2021年11月18日15:41:15 插入2800万以后的数据,进行查询 没有的才插入,有就先不更新了
        //先删除 后插入

        DSFactory.setCurrentDSFactory(new DruidDSFactory());
        DataSource ds = DSFactory.get("server_local");
        int startPmid = 0;
        Db use = Db.use(ds);
        String allDate = "select max(pmid) as pmid  from " + dbName;
        Entity query = use.query(allDate).get(0);
        SmallTool.printMess("最大的id 是" + query.getStr("pmid"));

        Integer count = query.getInt("pmid");
//        Integer count = 5000000;
        int breakSize = 100000;
        int pageSize = count / breakSize + 1;
        SmallTool.printMess("全部页码+++++++++" + pageSize);
//        SmallTool.printMess("即将开始请提前关闭如果想的话");
//        Thread.sleep(10000);

        SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");
        for (int i = 1; i <= pageSize; i++) {
            SmallTool.printMemory();
            SmallTool.printMess("当前页码+++++" + i);
            int start = breakSize * (i - 1) + 1;
            int end = i * breakSize;
            SmallTool.printMess("开始id++" + start + "+++结束id++++" + end);
            if (end <= startPmid) {
                SmallTool.printMess("因为小于" + startPmid + " 略过了");
                continue;
            }
            String paramSql = "select * from " + dbName + " where  pmid between  " + start + " and " + end;
            List<Entity> querys = use.query(paramSql);
            querys.parallelStream().forEach(entity -> {
                String content = ZipUtil.unGzip(entity.getBytes("content"), "utf-8");
                //解析html
                Document doc = Jsoup.parse(content);
                try {
                    // 主要是用于进行过滤掉参文房子参文doi混入文章doi
                    doc.select("ReferenceList").remove();
                } catch (Exception e) {

                }
                List<Element> divList = new ArrayList<>();
                Elements pubmedArticles = doc.select("PubmedArticle");
                Elements pubmedBookArticles = doc.select("PubmedBookArticle");
                for (Element e : pubmedArticles) {
                    divList.add(e);
                }
                for (Element e : pubmedBookArticles) {
                    divList.add(e);
                }
                List<Pubmed> pubmeds = PubmedUtils2.parseHtml(divList);
                //直接入库
                for (Pubmed pubmed : pubmeds) {
                    if (pubmed.getPmid() <= startPmid) {
                        SmallTool.printMess("小于开始 略过");
                        continue;
                    } else {
                        try {
                            insertData(pubmed, use);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
        }

    }

    @SneakyThrows
    public static void insertData(Pubmed pubmed, Db use) {
        if (insert) {
            use.insert(new Entity().setTableName(insetTable)
                    .set("pmid", pubmed.getPmid())
                    .set("type", pubmed.getType())
                    .set("article_title", pubmed.getArticleTitle())
                    .set("source", pubmed.getSource())
                    .set("intro", pubmed.getIntro())
                    .set("jour", pubmed.getJour())
                    .set("volume", pubmed.getVolume())
                    .set("issue", pubmed.getIssue())
                    .set("pages", pubmed.getPages())
                    .set("vip", pubmed.getVip())
                    .set("pub_date", pubmed.getPubDate())
                    .set("medline_date", pubmed.getMedlineDate())
                    .set("pub_year", pubmed.getPubYear())
                    .set("med_nlm_id", pubmed.getMedNlmId())
                    .set("author", pubmed.getAuthor())
                    .set("author_extend", pubmed.getAuthorExtend())
                    .set("affs", pubmed.getAffs())
                    .set("pmcid", pubmed.getPmcid())
                    .set("doi", pubmed.getDoi())
                    .set("lan", pubmed.getLan())
                    .set("ab_text", pubmed.getAbText())
                    .set("copyright", pubmed.getCopyright())
                    .set("keyword", pubmed.getKeyword())
                    .set("pts", pubmed.getPts())
                    .set("meshs", pubmed.getMeshs())
                    .set("mesh_heading", pubmed.getMeshHeading())
                    .set("mesh_headingy", pubmed.getMeshHeadingy())
                    .set("subs", pubmed.getSubs())
                    .set("grants", pubmed.getGrants())
                    .set("ass_date", pubmed.getAssDate())
                    .set("full_text", pubmed.getFullText())
                    .set("art_type", pubmed.getArtType())
            );
        } else {
            String sqlParam = "select * from " + insetTable + " where pmid = ?";
            Entity entity = use.queryOne(sqlParam, pubmed.getPmid());
            if (entity == null) {
                use.insert(new Entity().setTableName(insetTable)
                        .set("pmid", pubmed.getPmid())
                        .set("type", pubmed.getType())
                        .set("article_title", pubmed.getArticleTitle())
                        .set("source", pubmed.getSource())
                        .set("intro", pubmed.getIntro())
                        .set("jour", pubmed.getJour())
                        .set("volume", pubmed.getVolume())
                        .set("issue", pubmed.getIssue())
                        .set("pages", pubmed.getPages())
                        .set("vip", pubmed.getVip())
                        .set("pub_date", pubmed.getPubDate())
                        .set("medline_date", pubmed.getMedlineDate())
                        .set("pub_year", pubmed.getPubYear())
                        .set("med_nlm_id", pubmed.getMedNlmId())
                        .set("author", pubmed.getAuthor())
                        .set("author_extend", pubmed.getAuthorExtend())
                        .set("affs", pubmed.getAffs())
                        .set("pmcid", pubmed.getPmcid())
                        .set("doi", pubmed.getDoi())
                        .set("lan", pubmed.getLan())
                        .set("ab_text", pubmed.getAbText())
                        .set("copyright", pubmed.getCopyright())
                        .set("keyword", pubmed.getKeyword())
                        .set("pts", pubmed.getPts())
                        .set("meshs", pubmed.getMeshs())
                        .set("mesh_heading", pubmed.getMeshHeading())
                        .set("mesh_headingy", pubmed.getMeshHeadingy())
                        .set("subs", pubmed.getSubs())
                        .set("grants", pubmed.getGrants())
                        .set("ass_date", pubmed.getAssDate())
                        .set("full_text", pubmed.getFullText())
                        .set("art_type", pubmed.getArtType())
                );
            } else {
                Entity delE = new Entity();
                delE.setTableName(insetTable);
                delE.set("pmid", pubmed.getPmid());
                Entity entity1 = new Entity();
                entity1.set("pmid", pubmed.getPmid())
                        .set("type", pubmed.getType())
                        .set("article_title", pubmed.getArticleTitle())
                        .set("source", pubmed.getSource())
                        .set("intro", pubmed.getIntro())
                        .set("jour", pubmed.getJour())
                        .set("volume", pubmed.getVolume())
                        .set("issue", pubmed.getIssue())
                        .set("pages", pubmed.getPages())
                        .set("vip", pubmed.getVip())
                        .set("pub_date", pubmed.getPubDate())
                        .set("medline_date", pubmed.getMedlineDate())
                        .set("pub_year", pubmed.getPubYear())
                        .set("med_nlm_id", pubmed.getMedNlmId())
                        .set("author", pubmed.getAuthor())
                        .set("author_extend", pubmed.getAuthorExtend())
                        .set("affs", pubmed.getAffs())
                        .set("pmcid", pubmed.getPmcid())
                        .set("doi", pubmed.getDoi())
                        .set("lan", pubmed.getLan())
                        .set("ab_text", pubmed.getAbText())
                        .set("copyright", pubmed.getCopyright())
                        .set("keyword", pubmed.getKeyword())
                        .set("pts", pubmed.getPts())
                        .set("meshs", pubmed.getMeshs())
                        .set("mesh_heading", pubmed.getMeshHeading())
                        .set("mesh_headingy", pubmed.getMeshHeadingy())
                        .set("subs", pubmed.getSubs())
                        .set("grants", pubmed.getGrants())
                        .set("ass_date", pubmed.getAssDate())
                        .set("full_text", pubmed.getFullText())
                        .set("art_type", pubmed.getArtType());
                use.update(entity1, delE);
            }
        }
    }
}
